﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace Ewts.Storage
{
    public partial class RubbishData : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Request["loadid"] != null && Request["loadid"].ToString().Trim() != "")
                {
                    string loadid = Request["loadid"].ToString().Trim();
                    hiddenLoadID.Value = loadid;
                    DataLib.Model.LoadNum loadModel = new DataLib.BLL.LoadNum().GetModel(int.Parse(loadid));
                    if (loadModel != null)
                    {
                        loadRub.InnerHtml = loadModel.loadnum;
                        tdCollDate.InnerHtml = DateTime.Parse(loadModel.collectdate.ToString()).ToString("yyyy-MM-dd");
                        DataTable dt = new DataLib.BLL.Rubbish().GetAllList().Tables[0];
                        //dropRD.DataSource = dt;
                        //dropRD.DataTextField = "itemCode";
                        //dropRD.DataValueField = "itemName";
                        //dropRD.DataBind();
                        DataLib.Model.Cumtomer cumModel = new DataLib.BLL.Cumtomer().GetModel((int)loadModel.customerid);
                        if (cumModel != null)
                        {
                            tdCumCode.InnerHtml = cumModel.customercode;
                            tdCumName.InnerText = cumModel.companyname;
                        }
                        if (Request["mode"] != null && Request["mode"].ToString() == "update")
                        {
                            DataLib.Model.RubbishData rdmodel = new DataLib.BLL.RubbishData().GetModelByLoad((int)loadModel.id);
                            txtLDH.Value = rdmodel.LDH;
                            hiddenLoadID.Value = rdmodel.loadlnumid.ToString();
                            loadRub.InnerHtml = rdmodel.LoadNum;
                            txtRemark.Value = rdmodel.Remark;
                            txtHWCode.Text = rdmodel.rubbiscode;
                            labrubName.InnerHtml = rdmodel.rubbisname;
                            hiddenRubID.Value = rdmodel.rubbishid.ToString();
                            rubWeight.Value = rdmodel.rubbWeight.ToString();
                            rubhdataid.Value = rdmodel.id.ToString();
                            hidHwName.Value = rdmodel.rubbisname;
                        }

                    }
                    //labrubName.InnerHtml = dropRD.SelectedValue;



                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            DataLib.Model.RubbishData rdModel = new DataLib.Model.RubbishData();
            if (Request["mode"] != null && Request["mode"].ToString() == "update" && rubhdataid.Value!="0")
            {
                rdModel = new DataLib.BLL.RubbishData().GetModel(int.Parse(rubhdataid.Value));
            }
            rdModel.LDH = txtLDH.Value;
            rdModel.loadlnumid = int.Parse(hiddenLoadID.Value);
            rdModel.LoadNum = loadRub.InnerHtml;
            rdModel.Remark = txtRemark.Value;
            rdModel.rubbiscode = txtHWCode.Text;
            rdModel.rubbisname = hidHwName.Value;
            rdModel.rubbishid = int.Parse(hidhwid.Value);
            rdModel.rubbWeight = decimal.Parse(rubWeight.Value);
            rdModel.id = int.Parse(rubhdataid.Value);
            DataLib.BLL.RubbishData bll = new DataLib.BLL.RubbishData();
            if (Request["mode"] != null && Request["mode"].ToString() == "add")
            {
                if (bll.Add(rdModel) >= 0)
                {
                    DataLib.Common.MessageBox.ShowAndRedirect(Page, "录入完成", "RubbishDataList.aspx");
                }
            }
            else
            {
                if (bll.Update(rdModel) ==true)
                {
                    DataLib.Common.MessageBox.ShowAndRedirect(Page, "修改成功", "RubbishDataList.aspx");
                }
            }
        }

      
    }
}